<?php
/**
 * 本地化类
 * 
 * 配置文件默认存放在config/locale/$locale下，类似于
 * <pre>
 *   /config/locale/zh_CN
 *        config.php
 *        message.php
 * </pre>
 * 
 * 可以在config.php配置文件中添加
 *   $config["key"] = "value";
 * 如果上面的key是headers，如
 * <code>
 *  $config["headers"] = array(
 *		"content-type" => "text/html;charset=utf-8"
 *	); 
 * </code>
 * 则会自动将这些报头发送到浏览器端
 * 
 * 可以在message.php配置文件中添加
 *    $message["key"] = "value";
 * 
 *
 * @package util.core
 * @since 0.2.1
 */
class ILocale extends IObject {
	public $message = array();
	public $config = array();
	private $locale;
	private static $defaultLocale;
	
	/**
	 * 默认参数值
	 * 
	 * 用于fromXxx中的判断
	 *
	 * @since 0.2.1
	 */
	const LOCALE_PARAM = "if_locale";
	
	/**
	 * 所有已经设置的区域
	 *
	 * @var array
	 * @since 0.2.1
	 */
	private static $locales = array();
	
	/**
	 * 构造器
	 *
	 * @param string $locale Locale名称，如en_US,zh_CN
	 * @param string $dataDir 数据目录
	 * @since 0.2.1
	 */
	public function __construct($locale, $dataDir = null) {
		$this->locale = $locale;	
		self::$locales[] = $this;
		
		if ($dataDir == null) {
			$dataDir = IFRAMEWORK_CONFIG_DIRECTORY . "/locale/{$locale}";
		}
		
		if (!is_dir($dataDir) && self::$defaultLocale != null) {
			$dataDir = IFRAMEWORK_CONFIG_DIRECTORY . "/locale/" . self::$defaultLocale;
		}
		
		//加载config
		$configFile = $dataDir . "/config.php";
		if (is_file($configFile)) {
			require_once($configFile);
			if (isset($config) && is_array($config)) {
				$this->config = $config;
			}
		}
		
		//加载message
		$messageFile = $dataDir . "/message.php";
		if (is_file($messageFile)) {
			require_once($messageFile);
			if (isset($message) && is_array($message)) {
				$this->message = $message;
			}
		}
	}
	
	/**
	 * 区域名
	 *
	 * @return string
	 * @since 0.2.1
	 */
	public function getLocale() {
		return $this->locale;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.2.1
	 */
	public function getHeaders() {
		if (isset($this->config["headers"]) && is_array($this->config["headers"])) {
			return $this->config["headers"];
		}
		return array();
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.2.1
	 */
	public function sendHeaders() {
		if (headers_sent()) {
			return false;
		}
		foreach ($this->getHeaders() as $name => $content) {
			header($name . ":" . $content);
		}
		return true;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.2.1
	 */
	public function getMessages() {
		return $this->message;	
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.2.1
	 */
	public function getConfigs() {
		return $this->config;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $key
	 * @return unknown
	 * @since 0.2.1
	 */
	public function getMessage($key) {
		return array_key_exists($key, $this->message)?$this->message[$key]:null;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $key
	 * @return unknown
	 * @since 0.2.1
	 */
	public function getConfig($key) {
		return array_key_exists($key, $this->config)?$this->config[$key]:null; 
	}
	
	/**
	 * 设置默认的区域
	 *
	 * @param string $defaultLocale 默认的区域名
	 * @since 0.2.1
	 */
	public static function setDefault($defaultLocale) {
		self::$defaultLocale = $defaultLocale;
	}
	
	/**
	 * 从浏览器取得本地化对象
	 * 
	 * @return ILocale
	 * @since 0.2.1
	 */
	public static function fromBrowser($defaultLocale = null) {
		if ($defaultLocale != null) {
			self::setDefault($defaultLocale);
		}
		list($langStr) = explode(";", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
		$langs = explode(",", $langStr);
		if (empty($langs)) {
			if (self::$defaultLocale == null) {
				throw new IException("failed to get locale from browser");
			}
			else {
				return new ILocale(self::$defaultLocale);
			}
		}
		else {
			$locale = $langs[0];
			if (strstr($locale, "-")) {
				list($lang, $country) = explode("-", $locale);
				$locale = $lang . "_" . strtoupper($country);
			}
			return new ILocale($locale);
		}
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $cookieName cookie名
	 * @return ILocale
	 * @since 0.2.1
	 */
	public static function fromCookie($cookieName = null) {
		if (is_null($cookieName)) {
			$cookieName = self::LOCALE_PARAM;
		}
		$locale = ICookie::get($cookieName);
		if ($locale == null) {
			$locale = self::$defaultLocale;
		}
		return new ILocale($locale);
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $paramName 参数名
	 * @return ILocale
	 * @since 0.2.1
	 */
	public static function fromUrl($paramName) {
		$locale = in::getInstance()->get($paramName);
		if ($locale == null) {
			$locale = self::$defaultLocale;
		}
		return new ILocale($locale);
	}
	
	/**
	 * 取得当前设置过的区域
	 *
	 * @return array
	 * @since 0.2.1
	 */
	public static function getLocales() {
		return self::$locales;
	}
}

?>